using IcsSEMCtrl.Service.Logger;
using Lifter.Gui.Monitor.Popups;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using TKT.Lifter.CIM.Lifter.Device.Service.Alarm;

namespace Lifter.Utility
{
    public class Util
    {
        public struct SYSTEMTIME
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMilliseconds;
        }
        public enum dateTimeFormat
        {
            FULL,
            YYYYMMDD,
            YYMMDD,
            HHMMSS,
            YYYY_MM_DD,
            YY_MM_DD,
            HH_MM_SS,
            YYMMDDHHMMSS,
            YYYYMMDDHHMMSS,
            YYYYMMDDHHMMSS_COLRON,
            UNIQTIME,
            yyyy_MM_ddHHmmss
        }
        public enum DateFormat
        {
            YEAR,
            MONTH,
            DAY,
            HOUR,
            MIN,
            SECOND
        }
        public const string CONST_ID_HEADER_UNK = "UNK";
        public const string CONST_ID_HEADER_DUP = "DUP";
        public const string CONST_SINGLE_VEHICLE = "VEHICLE:1";
        public const int CONST_ABNORMAL_CARRIERID_RULE_LENGTH = 3;
        public const int CONST_CARRID_LENGTH = 25;
        public const int CONST_SKTC_HOST_NAME_LENGTH = 8;
        [DllImport("Kernel32.dll")]
        public static extern bool SetLocalTime(ref Util.SYSTEMTIME Time);
        public static bool isNormalCST(string strCSTID)
        {
            return !strCSTID.StartsWith("UNK") && !strCSTID.StartsWith("DUP") && Util.isNormalCST_CUSTOMCHECK(strCSTID);
        }
        public static bool isNormalCST_CUSTOMCHECK(string strCSTID)
        {
            return true;
        }
        public static string GetNowDateTimeString(Util.dateTimeFormat format)
        {
            string result = null;
            switch (format)
            {
                case Util.dateTimeFormat.FULL:
                    result = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    break;
                case Util.dateTimeFormat.YYYYMMDD:
                    result = DateTime.Now.ToString("yyyyMMdd");
                    break;
                case Util.dateTimeFormat.YYMMDD:
                    result = DateTime.Now.ToString("yyMMdd");
                    break;
                case Util.dateTimeFormat.HHMMSS:
                    result = DateTime.Now.ToString("HHmmss");
                    break;
                case Util.dateTimeFormat.YYYY_MM_DD:
                    result = DateTime.Now.ToString("yyyy-MM-dd");
                    break;
                case Util.dateTimeFormat.YY_MM_DD:
                    result = DateTime.Now.ToString("yy-MM-dd");
                    break;
                case Util.dateTimeFormat.HH_MM_SS:
                    result = DateTime.Now.ToString("HH:mm:ss");
                    break;
                case Util.dateTimeFormat.YYMMDDHHMMSS:
                    result = DateTime.Now.ToString("yyMMddHHmmss");
                    break;
                case Util.dateTimeFormat.YYYYMMDDHHMMSS:
                    result = DateTime.Now.ToString("yyyyMMddHHmmss");
                    break;
                case Util.dateTimeFormat.YYYYMMDDHHMMSS_COLRON:
                    result = DateTime.Now.ToString("yyyy:MM:dd:HH:mm:ss");
                    break;
                case Util.dateTimeFormat.UNIQTIME:
                    result = DateTime.Now.ToString("yyyyMMddHHmmssff");
                    break;
                case Util.dateTimeFormat.yyyy_MM_ddHHmmss:
                    result = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    break;
            }
            return result;
        }
        public static string CreateCarrierID(string header)
        {
            string matterialID = "";
            try
            {
                //获取时间戳方式
                //string dateTicks = DateTime.Now.Ticks.ToString();
                //精确到毫秒方式
                string dateTime = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                if (string.IsNullOrEmpty(header))
                {

                    matterialID = "Error" + dateTime;
                }
                else
                {
                    matterialID = header + dateTime;
                }
            }
            catch (Exception ex)
            {
                LogHelper.loggerError("在[CreateCarrierID]方法中报错", ex);
            }
            return matterialID;
        }
        public static int GetGapTimetoNow(DateTime xOldTime)
        {
            return (int)DateTime.Now.Subtract(xOldTime).TotalSeconds;
        }
        public static int DayCompare(DateTime dayA, DateTime dayB)
        {
            int result;
            if (dayA.Year == dayB.Year)
            {
                if (dayA.Month == dayB.Month)
                {
                    if (dayA.Day == dayB.Day)
                    {
                        result = 0;
                    }
                    else
                    {
                        if (dayA.Day > dayB.Day)
                        {
                            result = -1;
                        }
                        else
                        {
                            result = 1;
                        }
                    }
                }
                else
                {
                    if (dayA.Month > dayB.Month)
                    {
                        result = -1;
                    }
                    else
                    {
                        result = 1;
                    }
                }
            }
            else
            {
                if (dayA.Year > dayB.Year)
                {
                    result = -1;
                }
                else
                {
                    result = 1;
                }
            }
            return result;
        }
        public static string HexToBinary(string value, int length)
        {
            string text = Convert.ToString(Convert.ToInt32(value, 16), 2);
            if (text.Length > length)
            {
                text = text.Substring(0, length);
            }
            return text.PadLeft(length, '0');
        }
        public static string IntToBinary(int value, int length)
        {
            return Util.HexToBinary(Util.IntToHex(value), length);
        }
        public static string BinaryToHex(string value)
        {
            return Convert.ToInt32(value, 2).ToString("X");
        }
        public static string StringToHex(string s)
        {
            string text = "";
            string text2 = "";
            string result;
            if (s.Equals(""))
            {
                result = "0000";
            }
            else
            {
                int i;
                for (i = 0; i < s.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        text2 = Util.StringFormat(s[i]);
                    }
                    else
                    {
                        text2 = text2.Substring(0, 2) + Util.StringFormat(s[i]).Substring(0, 2);
                        text = text.Insert(text.Length, text2);
                    }
                }
                if (i % 2 != 0)
                {
                    text = text.Insert(text.Length, text2);
                }
                result = text;
            }
            return result;
        }
        private static string StringFormat(char s)
        {
            string text = "";
            int num = (int)s;
            string text2 = num.ToString("X");
            text2 = text2.PadRight(4, '0');
            return text.Insert(text.Length, text2);
        }
        public static string HexToDec(string s)
        {
            Console.WriteLine("HexToDec:CALLED");
            string result = "";
            try
            {
                result = Convert.ToInt64(s, 16).ToString();
            }
            catch (Exception ex)
            {
                result = ex.Message.ToString();
            }
            return result;
        }
        public static string IntToHex(int s)
        {
            Console.WriteLine("IntToHex:CALLED");
            string text = "";
            try
            {
                text = ((ushort)s).ToString("X");
                text = text.PadLeft(4, '0');
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return text;
        }
        public static string DecToHex(string s)
        {
            string text = "";
            try
            {
                string text2 = long.Parse(s).ToString("X");
                if (text2.Length < 4)
                {
                    for (int i = 0; i < 4 - text2.Length; i++)
                    {
                        text += "0";
                    }
                }
                text += text2;
            }
            catch (Exception ex)
            {
                text = ex.Message.ToString();
            }
            return text;
        }
        public static string HexToAscii(string _value)
        {
            string text = "";
            for (int i = 0; i < _value.Length; i += 4)
            {
                string str = "";
                string text2 = _value.Substring(i, 4);
                for (int j = 0; j < text2.Length; j += 2)
                {
                    str = Convert.ToChar(Convert.ToInt32(text2.Substring(j, 2), 16)).ToString() + str;
                }
                text += str;
            }
            return text;
        }
        public static string HexToString(string _value)
        {
            string text = "";
            string result;
            if (_value.Length < 2)
            {
                result = "";
            }
            else
            {
                for (int i = 0; i < _value.Length; i += 2)
                {
                    text += Convert.ToChar(Convert.ToInt32(_value.Substring(i, 2), 16)).ToString();
                }
                result = text;
            }
            return result;
        }
        public static string AsciiToHex(string _value)
        {
            string text = "";
            for (int i = 0; i < _value.Length; i += 2)
            {
                string text2 = _value.Substring(i, 2);
                char[] array = text2.ToCharArray();
                text2 = "";
                text2 += Convert.ToString((int)array[1], 16);
                text2 = text2.PadLeft(2, '0');
                text = text.Insert(text.Length, text2);
                text2 = Convert.ToString((int)array[0], 16);
                text2 = text2.PadLeft(2, '0');
                text = text.Insert(text.Length, text2);
            }
            return text;
        }
        public static int GetGapTime1toNow(Util.DateFormat df, string time1)
        {
            int result = 0;
            try
            {
                DateTime now = DateTime.Now;
                DateTime value = Convert.ToDateTime(time1);
                TimeSpan ts = now.Subtract(value);
                result = Util.GetValueTimeSpan(df, ts);
            }
            catch
            {
            }
            return result;
        }
        public static int GetGapTime1toNow(Util.DateFormat df, DateTime time1)
        {
            int result = 0;
            try
            {
                TimeSpan ts = DateTime.Now.Subtract(time1);
                result = Util.GetValueTimeSpan(df, ts);
            }
            catch
            {
            }
            return result;
        }
        public static int GetValueTimeSpan(Util.DateFormat df, TimeSpan ts)
        {
            int result = 0;
            switch (df)
            {
                case Util.DateFormat.YEAR:
                    result = (int)(ts.TotalDays / 365.0);
                    break;
                case Util.DateFormat.MONTH:
                    result = (int)(ts.TotalDays / 30.0);
                    break;
                case Util.DateFormat.DAY:
                    result = (int)ts.TotalDays;
                    break;
                case Util.DateFormat.HOUR:
                    result = (int)ts.TotalHours;
                    break;
                case Util.DateFormat.MIN:
                    result = (int)ts.TotalMinutes;
                    break;
                case Util.DateFormat.SECOND:
                    result = (int)ts.TotalSeconds;
                    break;
            }
            return result;
        }
        public static string StringReverse(string source)
        {
            string text = "";
            for (int i = 0; i < source.Length; i++)
            {
                text += source.Substring(source.Length - (i + 1), 1);
            }
            return text;
        }
        /// <summary>
        /// add by yangshl
        /// 通过指定成员，将对象list转成指定成员的stringlist
        /// </summary>
        /// <param name="objectList"></param>
        /// <param name="memberName"></param>
        /// <returns></returns>
        public static List<string> ObjectListToStringList(List<AlarmDao> objectList, string memberName)
        {
            List<string> MemberList = new List<string>();
            if (objectList != null && objectList.Count != 0)
            {
                foreach (var item in objectList)
                {
                    if ("IS_HOST_REPORT".Equals(memberName))
                    {
                        MemberList.Add(item.IS_HOST_REPORT);
                    }
                    else if ("SUBSYSTEM".Equals(memberName))
                    {
                        MemberList.Add(item.SUBSYSTEM);
                    }
                }
            }
            return MemberList;
        }
    }
}
